﻿using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Text;

using System.Data.SQLite;

using WFL.Data.SQLite;

namespace WFL.Data.SQLite
{
    public class SQLiteKeyValueConfig
    {
        private readonly string _dataSource;
        private SQLiteOperation _operation;

        public SQLiteKeyValueConfig(string dataSource)
        {
            _dataSource = dataSource;
            _operation = new SQLiteOperation("data source=" + _dataSource);

            Init();
        }

        private void Init()
        {
            if (!File.Exists(_dataSource))
            {
                FileStream fs = File.Create(_dataSource);
                fs.Close();
                fs = null;
                CreateConfigTable();
            }
            else
            {
                string sql = "select count(*) from sqlite_master where type='table' and name=?";
                SQLiteParameter[] ps = new SQLiteParameter[1];
                ps[0] = new SQLiteParameter("name", "b125068c3ac547fa8591450132ce01c1");
                int result = Convert.ToInt32(_operation.ExecuteScalar(sql, ps));
                if (result == 0)
                {
                    CreateConfigTable();
                }
            }
        }

        private void CreateConfigTable()
        {
            //创建配置表
            string sql = "create table [b125068c3ac547fa8591450132ce01c1] ([Key] nvarchar(32) not null,[Value] nvarchar(128) null,primary key([Key]) );";
            _operation.ExecuteNonQuery(sql);
        }

        public bool Exists(string key)
        {
            string sql = "select value from b125068c3ac547fa8591450132ce01c1 where key=?";
            SQLiteParameter[] ps = new SQLiteParameter[1];
            ps[0] = new SQLiteParameter("key", key);
            object obj = _operation.ExecuteScalar(sql, ps);
            return (obj != null);
        }

        public string Get(string key)
        {
            string value = null;
            Get(key, out value);
            return value;
        }

        public bool Get(string key, out string value)
        {
            value = null;
            string sql = "select value from b125068c3ac547fa8591450132ce01c1 where key=?";
            SQLiteParameter[] ps = new SQLiteParameter[1];
            ps[0] = new SQLiteParameter("key", key);
            object obj = _operation.ExecuteScalar(sql, ps);
            if (obj != null)
            {
                if (!(obj is DBNull))
                {
                    value = obj.ToString();
                }
                return true;
            }
            else
            {
                return false;
            }
        }

        public void Set(string key, string value)
        {
            Remove(key);

            string sql = "insert into b125068c3ac547fa8591450132ce01c1 values(?,?)";
            SQLiteParameter[] ps = new SQLiteParameter[2];
            ps[0] = new SQLiteParameter("key", key);
            ps[1] = new SQLiteParameter("value", value);
            _operation.ExecuteNonQuery(sql, ps);
        }

        public bool Remove(string key)
        {
            string sql = "delete from b125068c3ac547fa8591450132ce01c1 where key=?";
            SQLiteParameter[] ps = new SQLiteParameter[1];
            ps[0] = new SQLiteParameter("key", key);
            return (_operation.ExecuteNonQuery(sql, ps) == 1);
        }

        public void Clear()
        {
            string sql = "delete from b125068c3ac547fa8591450132ce01c1";
            _operation.ExecuteNonQuery(sql);
        }


    }
}
